package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.PublicAuditListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.PackageDetailListEntity;
import com.glory.dgcpcpda.utils.LotUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class OuterBoxCheckActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etJobNo)
    FreeUI_ClearEditText etJobNo;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.etOuterBoxID)
    FreeUI_ClearEditText etOuterBoxID;
    @BindView(R.id.etInnerBoxID)
    FreeUI_ClearEditText etInnerBoxID;
    @BindView(R.id.tvAdd)
    TextView tvAdd;
    @BindView(R.id.rvMaterielList)
    RecyclerView rvMaterielList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private PublicAuditListAdapter mPublicAuditListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_outer_box_check;
    }

    @Override
    protected void initView() {
        tvTitle.setText("外箱审核");
        setMainLayout(llMain);
        initRecyclerView(rvMaterielList,this);
        mPublicAuditListAdapter = new PublicAuditListAdapter(null,false);
        mPublicAuditListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mPublicAuditListAdapter.isFirstOnly(true);
        rvMaterielList.setAdapter(mPublicAuditListAdapter);

        initEdit(etInnerBoxID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                if (getText(etLotID).equals("") || getText(etOuterBoxID).equals("")){
                    showToast("批次号或外箱ID不能为空",false);
                    etInnerBoxID.setText("");
                    return;
                }

                LotUtils.publicAudit(OuterBoxCheckActivity.this, getSupportFragmentManager(), getText(etLotID), getText(etOuterBoxID), "CASE", new LotUtils.netCallback<PackageDetailListEntity>() {
                    @Override
                    public void onSuccess(PackageDetailListEntity response) {
                        if (isHavePackID(response)){
                            mPublicAuditListAdapter.addData(response);
                            etOuterBoxID.setText("");
                            etInnerBoxID.setText("");
                        }
                    }

                    @Override
                    public void onFail(Throwable t) {
                        loadError(t,"PUBLICAUDIT");
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (actionControl("Submit15"))return;
                if (mPublicAuditListAdapter.getData().size() <= 0){
                    showToast("请先添加核对信息",false);
                    return;
                }

                LotUtils.publicAudit(this,getSupportFragmentManager(),"CASE","CASE",mPublicAuditListAdapter.getData());
                break;
//            case R.id.tvAdd:
//                if (getText(etLotID).equals("") || getText(etOuterBoxID).equals("")){
//                    showToast("批次号或外箱ID不能为空",false);
//                    return;
//                }
//
//                LotUtils.publicAudit(this, getSupportFragmentManager(), getText(etLotID), getText(etOuterBoxID), "CASE", new LotUtils.netCallback<PackageDetailListEntity>() {
//                    @Override
//                    public void onSuccess(PackageDetailListEntity response) {
//                        if (isHavePackID(response)){
//                            mPublicAuditListAdapter.addData(response);
//                            etLotID.setText("");
//                            etOuterBoxID.setText("");
//                        }
//                    }
//
//                    @Override
//                    public void onFail(Throwable t) {
//                        loadError(t,"PUBLICAUDIT");
//                    }
//                });
//                break;
        }

    }

    private boolean isHavePackID(PackageDetailListEntity response){
        if (mPublicAuditListAdapter.getData().size() <= 0){
            return true;
        }

        for (PackageDetailListEntity entity : mPublicAuditListAdapter.getData()){
            if (entity.getPACKLOTID().equals(response.getPACKLOTID())){
                showToast("当前包装条码已在列表中",true);
                return false;
            }
        }

        return true;
    }

}
